#include <map>
#include "sym-name-table.h"
#include "log-util.h"
#include "autocall-util.h"

using std::string;
using std::map;
using std::pair;


namespace {

map<string,pair<SymNameTable*,STRH::RegisterFunc>> g_register_langs;
void destory_register_langs()
{
    for (const auto &var : g_register_langs)
        if (var.second.first != NULL)
            delete var.second.first;
    return ;
}
AUTOCALL(destory_register_langs);


}

SymNameTable*
SymNameTable::instance(const std::string &language)
{
    auto ptr = g_register_langs.find(language);
    if (ptr == g_register_langs.end())
        return NULL;
    if (ptr->second.first == NULL)
        ptr->second.first = ptr->second.second();
    return ptr->second.first;
}

std::vector<std::string>
SymNameTable::supportLanguages()
{
    std::vector<std::string> register_languages;
    for (const auto &var : g_register_langs)
        register_languages.push_back(var.first);
    return register_languages;
}


STRH::STRH(const std::string &l,RegisterFunc f_ptr)
{
    auto ptr = g_register_langs.find(l);
    if (ptr != g_register_langs.end())
        LOGE("语言 %s 已经存在,不需要再次插入!",l.c_str());

    g_register_langs[l].first = 0;
    g_register_langs[l].second = f_ptr;
    return ;
}
